package com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;


/**
 */
public class ARResponse {
	private String errorMsg = "=空数据=";
	private int errorNo = -2;
	private JSONObject data;

	public String getErrorMsg() {
		return this.errorMsg;
	}

	public int getErroNo() {
		return errorNo;
	}

	public ARResponse(){}
	
	public ARResponse(String responseStr){
		setResponse(responseStr);
	}

	public ARResponse(String responseStr,String key){
		setResponse("{\""+key+"\":"+ responseStr +"}");
	}
	
	/**
	 * @描述： 数据化数据<br>
	 * @param responseStr
	 *            response字符对象
	 */
	public void setResponse(String responseStr) {
		try {
			JSONObject responseObj = new JSONObject(responseStr);
			setResponse(responseObj);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @描述：初始化数据 <br>
	 * @param responseObj
	 *            response JSON对象
	 */
	public void setResponse(JSONObject responseObj) {

		try {
			errorMsg = responseObj.optString("message");
			this.errorNo = responseObj.optInt("success");

			this.data = responseObj.optJSONObject("data");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getValue(String name) {
		return data.optString(name);
	}


	public boolean update(String name, String value) {
		try {
			data.put(name, value);
			return true;
		} catch (JSONException e) {
			return false;
		}
	}
	
	public int getRowNum(){
		if(data == null)
			return 0;
		else
			return 1;
	}

	public HashMap<String,String> getDataWithMap(){
		Gson gson = new Gson();
		HashMap<String,String> infoMap = gson.fromJson(getData(), new TypeToken<HashMap<String, String>>(){}.getType());
		return infoMap;
	}


	public String getData(){
		return data.toString();
	}
	
	public String toString(){
		return "{\"data\":"+ getData() +"}";
	}
}
